#!/usr/bin/env python
# coding=utf-8
# __author__ = 'Yunchao Ling'

from pymongo import MongoClient

SERVER = 'localhost'
PORT = 27017
DB_NAME = "pmc"
COLLECTION_NAME = "filelist"

connection = MongoClient(SERVER, PORT)
db = connection[DB_NAME]
collection = db[COLLECTION_NAME]

infile1 = open("oa_file_list.txt", "r")
outfile1 = open("oa_file_list.txt.miss", "w")
infile1.readline()
for line in infile1:
    line = line.rstrip("\n")
    splitline = line.split("\t")
    filepath = splitline[0][11:]
    result = collection.find_one({"path": filepath})
    if result == None:
        outfile1.write(line + "\n")
        outfile1.flush()
outfile1.close()
infile1.close()

infile2 = open("oa_comm_use_file_list.txt", "r")
outfile2 = open("oa_comm_use_file_list.txt.miss", "w")
infile2.readline()
for line in infile2:
    line = line.rstrip("\n")
    splitline = line.split("\t")
    filepath = splitline[0][11:]
    result = collection.find_one({"path": filepath})
    if result == None:
        outfile2.write(line + "\n")
        outfile2.flush()
outfile2.close()
infile2.close()

infile3 = open("oa_non_comm_use_pdf.txt", "r")
outfile3 = open("oa_non_comm_use_pdf.txt.miss", "w")
infile3.readline()
for line in infile3:
    line = line.rstrip("\n")
    splitline = line.split("\t")
    filepath = splitline[0][7:]
    result = collection.find_one({"path": filepath})
    if result == None:
        outfile3.write(line + "\n")
        outfile3.flush()
outfile3.close()
infile3.close()

connection.close()
